#pragma once

#include <QGraphicsView>
#include <QGraphicsScene>
#include <QGraphicsProxyWidget>
#include <QList>
#include <QMargins>
#include <QWidget>
#include <QSize>

enum class OverlayAlignment
{
    TopLeft,
    TopCenter,
    TopRight,
    CenterLeft,
    Center,
    CenterRight,
    BottomLeft,
    BottomCenter,
    BottomRight,
    Fill
};

/**
 * QOverlay - Qt版本的覆盖布局控件
 * 基于QGraphicsView实现，支持多个控件按Z顺序重叠显示，类似UE4的SOverlay
 */
class QOverlay : public QGraphicsView
{
    Q_OBJECT
    
public:
    /**
     * 覆盖槽 - 管理单个子控件的位置、对齐和Z顺序
     */
    class OverlaySlot
    {
    public:
        OverlaySlot(QWidget* widget = nullptr, qreal zValue = 0);
        ~OverlaySlot();
        
        // 基本属性
        QWidget* widget() const { return Widget; }
        QGraphicsProxyWidget* proxyWidget() const { return ProxyWidget; }
        void setWidget(QWidget* widget);
        
        qreal zValue() const { return ZValue; }
        void setZValue(qreal zValue);
        
        // 对齐方式
        OverlayAlignment alignment() const { return Alignment; }
        void setAlignment(OverlayAlignment alignment);
        
        // 边距
        QMargins padding() const { return Padding; }
        void setPadding(const QMargins& padding);
        void setPadding(int left, int top, int right, int bottom);
        
        // 尺寸约束
        QSize sizeHint() const { return SizeHintValue; }
        void setSizeHint(const QSize& size);
        
        // 可见性
        bool isVisible() const;
        void setVisible(bool visible);
        
        // 透明度
        qreal opacity() const;
        void setOpacity(qreal opacity);
        
    private:
        QWidget* Widget;
        QGraphicsProxyWidget* ProxyWidget;
        qreal ZValue;
        OverlayAlignment Alignment;
        QMargins Padding;
        QSize SizeHintValue;
        
        friend class QOverlay;
    };
    
public:
    explicit QOverlay(QWidget* parent = nullptr);
    ~QOverlay();
    
    // 槽管理
    OverlaySlot* addWidget(QWidget* widget, qreal zValue = -1, 
                          OverlayAlignment alignment = OverlayAlignment::TopLeft);
    OverlaySlot* addWidget(QWidget* widget, qreal zValue, 
                          OverlayAlignment alignment, const QMargins& padding);
    
    void removeWidget(QWidget* widget);
    void removeSlot(OverlaySlot* slot);
    void clearSlots();
    
    // Z顺序管理
    bool hasSlotWithZValue(qreal zValue) const;
    OverlaySlot* getSlotByZValue(qreal zValue) const;
    void moveSlotToFront(OverlaySlot* slot);
    void moveSlotToBack(OverlaySlot* slot);
    
    // 槽查询
    int slotCount() const;
    QList<OverlaySlot*> overlaySlots() const;
    OverlaySlot* slotAt(int index) const;
    
    // 便捷方法
    OverlaySlot* addCenteredWidget(QWidget* widget, qreal zValue = -1);
    OverlaySlot* addTopRightWidget(QWidget* widget, qreal zValue = -1, 
                                  const QMargins& padding = QMargins(0, 0, 10, 10));
    OverlaySlot* addBottomLeftWidget(QWidget* widget, qreal zValue = -1, 
                                    const QMargins& padding = QMargins(10, 10, 0, 0));

signals:
    void slotAdded(OverlaySlot* slot);
    void slotRemoved(OverlaySlot* slot);
    void zOrderChanged(OverlaySlot* slot);

protected:
    // QGraphicsView重写
    void resizeEvent(QResizeEvent* event) override;
    void childEvent(QChildEvent* event) override;
    QSize sizeHint() const override;
    QSize minimumSizeHint() const override;

private:
    void arrangeSlots();
    void updateSlotGeometry(OverlaySlot* slot);
    QRectF calculateSlotGeometry(OverlaySlot* slot, const QRectF& availableRect) const;
    void insertSlotByZValue(OverlaySlot* slot);
    void sortSlotsByZValue();
    
private:
    QGraphicsScene* Scene;
    QList<OverlaySlot*> Slots;
    bool NeedsArrange;
};

